﻿using Api.Shared.Model.Yanji;
using Api.Shared.MyPlc.S7;
using Api.Shared.Responsitory.Mysql;
using Api.Shared.Responsitory.Sqlite;
using Api.Shared.Sugar;
using Api.Shared.Sugar.Mysql;
using Api.Shared.Sugar.Sqlite;
using CommunityToolkit.Mvvm.Messaging;
using KxApp.Chart;
using KxApp.Utils;
using KxApp.ViewModels;
using KxApp.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Formatting.Json;
using System.Windows;

namespace KxApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        [STAThread]
        static void Main(string[] args)
        {
            using IHost? host = CreateHostBuilder(args).Build();
            host.Start();
            var app = new App();
            app.InitializeComponent();
            //app.MainWindow = host.Services.GetRequiredService<MainView>();
            app.MainWindow = host.Services.GetRequiredService<LoginView>();
            app.MainWindow.Visibility = Visibility.Visible;

            app.Run();
        }

        private static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureServices(services =>
                {
                    //services.AddSingleton<MainViewModel>();
                    services.AddSingleton<MainView>(sp =>
                        new() { DataContext = sp.GetRequiredService<MainViewModel>() }
                    );
                    services.AddSingleton<LoginViewModel>(sp =>
                        new() { Mv = sp.GetRequiredService<MainView>() }
                    );
                    services.AddSingleton<TableView>(sp =>
                        new() { DataContext = sp.GetRequiredService<TableViewModel>() }
                    );
                    services.AddSingleton<OpcTestView>(sp =>
                        new() { DataContext = sp.GetRequiredService<OpcTestViewModel>() }
                    );

                    services.AddSingleton<DeviceView>(sp =>
                        new() { DataContext = sp.GetRequiredService<DeviceViewModel>() }
                    );
                    services.AddSingleton<WeakReferenceMessenger>();
                    services.AddSingleton<IMessenger, WeakReferenceMessenger>(sp =>
                        sp.GetRequiredService<WeakReferenceMessenger>()
                    );
                    services.AddSingleton<ChartView>(sp =>
                        new() { DataContext = sp.GetRequiredService<ChartViewModel>() }
                    );
                    services.AddSingleton<ScanView>(sp =>
                        new() { DataContext = sp.GetRequiredService<ScanViewModel>() }
                    );
                    services.AddSingleton<IDeviceService, DeviceServiceImpl>();
                    services.AddSingleton<ApiData>();
                    services.AddSingleton<AbbApiData>();
                    services.AddSingleton<ChartService>();
                    services.AddSingleton<IS7ConnService, S7ConnServiceImpl>();
                    //services.AddSingleton(_ => Current.Dispatcher);
                    services.AddHostedService<StartupInitializationService>();
                    services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

                    services.AddHttpClient();
                    #region Sugar

                    services.AddKeyedSingleton<ISqlSugarService, SqliteService>(
                        nameof(SqlName.sqlite)
                    );
                    services.AddKeyedSingleton<ISqlSugarService, MySqlService>(
                        nameof(SqlName.mysql)
                    );

                    #region Scope
                    services.AddKeyedScoped<ISugarClientService, SqliteClientService>(
                        nameof(SqlName.sqlite)
                    );
                    services.AddKeyedScoped<ISugarClientService, MysqlClientService>(
                        nameof(SqlName.mysql)
                    );

                    //注入仓库 万能注入 AddScoped
                    services.AddScoped(typeof(SqliteRespository<>));
                    services.AddScoped(typeof(MysqlRespository<>));
                    #endregion

                    #region Singleton
                    services.AddKeyedSingleton<ISugarClientService, SqliteClientService>(
                        nameof(SqlName.sqliteST)
                    );
                    services.AddKeyedSingleton<ISugarClientService, MysqlClientService>(
                        nameof(SqlName.mysqlST)
                    );

                    //注入仓库 万能注入 AddSingleton
                    services.AddSingleton(typeof(SqliteSTRespository<>));
                    services.AddSingleton(typeof(MysqlSTRespository<>));
                    #endregion
                    #endregion

                    services.AddSingleton<IS7ConnService, S7ConnServiceImpl>();
                    services.RegisterAssembly();
                })
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    Log.Logger = new LoggerConfiguration()
                        .MinimumLevel.Debug()
                        .Enrich.FromLogContext()
                        .WriteTo.Console(new JsonFormatter())
                        .WriteTo.File(
                            new JsonFormatter(),
                            @"D:\serialLog\log-.txt",
                            rollingInterval: RollingInterval.Day
                        )
                        .CreateLogger();
                    logging.AddSerilog(Log.Logger);
                });
            ;
        }
    }
}
